﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Animation;

using Prism.Games.GalEngine.Scripts.Data.SceneElements;
using Prism.Core;
using Prism.Core.WPF;
using Prism.Games.GalEngine.Scripts.Parser.Input;
using System.Windows.Controls;
using System.IO;

namespace Prism.Games.GalEngine.Scripts.Parser
{
    public class VIParser : ISceneParser
    {
        public VIParser()
        {
            this.VICanvas = Helper.GraphicBase.FindName(Helper.GraphicBase.GalUIFrame.GamingFrame, "VerticalImagesCanvas") as System.Windows.Controls.Canvas;
        }

        public void Restart()
        {
            this.VICanvas.Children.Clear();
        }


        public void PresentObject(FunctionModel.SceneObject element)
        {
            if (element is VerticalImage)
            {
                var target = element as VerticalImage;

                if (target.IsCached)
                {
                    var image = new System.Windows.Controls.Image() { Source = target.CacheStream.CreateBitmapImage(), Stretch = System.Windows.Media.Stretch.None };
                    this.SetLocation(image, target);
                    this.VICanvas.Children.Add(image);

                    if (target.Effect != null)
                    {
                        // PlayAnimation
                        this.PlayEffect(image, target.Effect);
                    }
                }
                //else
                //{
                //    if (target.PartUri != null)
                //    {
                //        using (var ms = Helper.Packer.GetPartSteam(target.PartUri))
                //        {
                //            var image = new System.Windows.Controls.Image() { Source = Helper.CreateBitmapImage(ms), Stretch = System.Windows.Media.Stretch.None };
                //            this.SetLocation(image, target);
                //            this.VICanvas.Children.Add(image);

                //            if (target.Effect != null)
                //            {
                //                // PlayAnimation
                //                this.PlayEffect(image, target.Effect);
                //            }
                //        }
                //    }
                //}
            }
        }

        private void SetLocation(FrameworkElement element, VerticalImage target)
        {
            Canvas.SetLeft(element, target.Left);

            Canvas.SetTop(element, target.Top);
        }

        private void PlayEffect(System.Windows.FrameworkElement target, Prism.Games.GalEngine.Scripts.Data.Effect.Effect effect)
        {
            var storyBoard = new Storyboard();
            foreach (var i in effect.Animations)
            {
                if (i is Prism.Games.GalEngine.Scripts.Data.Effect.AlphaAnimation)
                {
                    var alphaAnimation = i as Prism.Games.GalEngine.Scripts.Data.Effect.AlphaAnimation;
                    var doubleAnimation = new DoubleAnimation();
                    Storyboard.SetTarget(doubleAnimation, target);
                    Storyboard.SetTargetProperty(doubleAnimation, new PropertyPath("Opacity"));
                    doubleAnimation.From = alphaAnimation.From;
                    doubleAnimation.To = alphaAnimation.To;
                    doubleAnimation.BeginTime = alphaAnimation.StartTime;
                    doubleAnimation.Duration = new Duration(alphaAnimation.EndTime - alphaAnimation.StartTime);

                    storyBoard.Children.Add(doubleAnimation);
                }
                else if (i is Prism.Games.GalEngine.Scripts.Data.Effect.MoveAnimation)
                {
                    var moveAnimation = i as Prism.Games.GalEngine.Scripts.Data.Effect.MoveAnimation;

                    var doubleAnimationL = new DoubleAnimation();
                    Storyboard.SetTarget(doubleAnimationL, target);
                    Storyboard.SetTargetProperty(doubleAnimationL, new PropertyPath(Canvas.LeftProperty));
                    doubleAnimationL.From = moveAnimation.From.Left;
                    doubleAnimationL.To = moveAnimation.To.Left;
                    doubleAnimationL.BeginTime = moveAnimation.StartTime;
                    doubleAnimationL.Duration = new Duration(moveAnimation.EndTime - moveAnimation.StartTime);
                    storyBoard.Children.Add(doubleAnimationL);

                    var doubleAnimationT = new DoubleAnimation();
                    Storyboard.SetTarget(doubleAnimationT, target);
                    Storyboard.SetTargetProperty(doubleAnimationT, new PropertyPath(Canvas.TopProperty));
                    doubleAnimationT.From = moveAnimation.From.Top;
                    doubleAnimationT.To = moveAnimation.To.Top;
                    doubleAnimationT.BeginTime = moveAnimation.StartTime;
                    doubleAnimationT.Duration = new Duration(moveAnimation.EndTime - moveAnimation.StartTime);
                    storyBoard.Children.Add(doubleAnimationT);
                }
            }

            storyBoard.Begin(target);
        }

        public System.Windows.Controls.Canvas VICanvas { get; set; }
    }
}
